var x_PI = 3.14159265358979324 * 3000.0 / 180.0;
var PI = 3.1415926535897932384626;
var a = 6378245.0;
var ee = 0.00669342162296594323;

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}


const ToDegrees = val => {
  if (typeof (val) == "undefined" || val == "") {
    return "";
  }
  var i = val.indexOf('.');
  var strDu = i < 0 ? val : val.substring(0, i); //获取度
  var strFen = 0;
  var strMiao = 0;
  if (i > 0) {
    var strFen = "0" + val.substring(i);
    strFen = strFen * 60 + "";
    i = strFen.indexOf('.');
    if (i > 0) {
      strMiao = "0" + strFen.substring(i);
      strFen = strFen.substring(0, i); //获取分
      strMiao = strMiao * 60 + "";
      i = strMiao.indexOf('.');
      strMiao = strMiao.substring(0, i + 4); //取到小数点后面三位
      strMiao = parseFloat(strMiao).toFixed(2); //精确小数点后面两位
    }
  }
  return strDu + "," + strFen + "," + strMiao; // 这里可以修改成你想要的格式例如你可以
  //    改成这样的：return strDu + "°" + strFen + "'" + strMiao + "\"N";
}

var ToDigital = {
  //度°分′秒″转度
  ToDigital(strDu, strFen, strMiao, len) {
    len = (len > 6 || typeof (len) == "undefined") ? 6 : len; //精确到小数点后最多六位   
    strDu = (typeof (strDu) == "undefined" || strDu == "") ? 0 : parseFloat(strDu);
    strFen = (typeof (strFen) == "undefined" || strFen == "") ? 0 : parseFloat(strFen) / 60;
    strMiao = (typeof (strMiao) == "undefined" || strMiao == "") ? 0 : parseFloat(strMiao) / 3600;
    var digital = strDu + strFen + strMiao;
    if (digital == 0) {
      return "";
    } else {
      return digital.toFixed(len);
    }
  },
}

let wgs84togcj02 = {

  wgs84togcj02(lng, lat) {
    if (outOfChina(lng, lat)) {
      return [lng, lat]
    } else {
      var dlat = transformlat(lng - 105.0, lat - 35.0)
      var dlng = transformlng(lng - 105.0, lat - 35.0)
      var radlat = lat / 180.0 * PI
      var magic = Math.sin(radlat)
      magic = 1 - ee * magic * magic
      var sqrtmagic = Math.sqrt(magic)
      dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI)
      dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI)
      const mglat = lat + dlat
      const mglng = lng + dlng
      return [mglng, mglat]
    }
  }

}

/**
 * 判断是否在国内，不在国内则不做偏移
 * @param {*} lng
 * @param {*} lat
 */
function outOfChina(lng, lat) {
  return (lng < 72.004 || lng > 137.8347) || ((lat < 0.8293 || lat > 55.8271) || false)
}

/**
 * 经度转换
 * @param { Number } lng
 * @param { Number } lat
 */
function transformlat(lng, lat) {
  var ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng))
  ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0
  ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0
  ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0
  return ret
}

/**
 * 纬度转换
 * @param { Number } lng
 * @param { Number } lat
 */
function transformlng(lng, lat) {
  var ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng))
  ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0
  ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0
  ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0
  return ret
}



let gcj02towgs84 = {
  gcj02towgs84(lng, lat) {
    if (outOfChina(lng, lat)) {
      return [lng, lat]
    } else {
      var dlat = transformlat(lng - 105.0, lat - 35.0)
      var dlng = transformlng(lng - 105.0, lat - 35.0)
      var radlat = lat / 180.0 * PI
      var magic = Math.sin(radlat)
      magic = 1 - ee * magic * magic
      var sqrtmagic = Math.sqrt(magic)
      dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI)
      dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI)
      const mglat = lat + dlat
      const mglng = lng + dlng
      return [lng * 2 - mglng, lat * 2 - mglat]
    }
  }

}

const formatWind = speed => {
  let windLevel = null
  let speedNum = Math.round(speed)
  if (speedNum < 1) {
    windLevel = 0;
  } else if (speedNum >= 1 && speedNum <= 5) {
    windLevel = 1;
  } else if (speedNum >= 6 && speedNum <= 11) {
    windLevel = 2;
  } else if (speedNum >= 12 && speedNum <= 19) {
    windLevel = 3;
  } else if (speedNum >= 20 && speedNum <= 28) {
    windLevel = 4;
  } else if (speedNum >= 29 && speedNum <= 38) {
    windLevel = 5;
  } else if (speedNum >= 39 && speedNum <= 49) {
    windLevel = 6;
  } else if (speedNum >= 50 && speedNum <= 61) {
    windLevel = 7;
  } else if (speedNum >= 62 && speedNum <= 74) {
    windLevel = 8;
  } else if (speedNum >= 75 && speedNum <= 88) {
    windLevel = 9;
  } else if (speedNum >= 89 && speedNum <= 102) {
    windLevel = 10;
  } else if (speedNum >= 103 && speedNum <= 117) {
    windLevel = 11;
  } else if (speedNum >= 118 && speedNum <= 133) {
    windLevel = 12;
  } else if (speedNum >= 134 && speedNum <= 149) {
    windLevel = 13;
  } else if (speedNum >= 150 && speedNum <= 166) {
    windLevel = 14;
  } else if (speedNum >= 167 && speedNum <= 183) {
    windLevel = 15;
  } else if (speedNum >= 184 && speedNum <= 201) {
    windLevel = 16;
  } else if (speedNum >= 202 && speedNum <= 220) {
    windLevel = 17;
  }
  return windLevel
}
// 获取UUID
function getUUID(n = 32) {
  var str = "abcdefghijklmnopqrstuvwxyz0123456789"; // 可以作为常量放到random外面
  var result = "";
  for (var i = 0; i < n; i++) {
    result += str[parseInt(Math.random() * str.length)];
  }
  return result;
}

module.exports = {
  formatTime,
  ToDegrees,
  ToDigital,
  formatWind,
  wgs84togcj02,
  gcj02towgs84,
  getUUID
}